﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Windows.Report
{
    public partial class frmBaoCaoNKGiaoDich : DevExpress.XtraEditors.XtraForm
    {
        public string sErr = "";
        Dictionary<string, object> _valueList = new Dictionary<string, object>();

        public Dictionary<string, object> ValueList
        {
            get { return _valueList; }
            set { _valueList = value; }
        }
        DataSet _ds = null;
        public DataSet DataSource { get { return _ds; } }


        public frmBaoCaoNKGiaoDich()
        {
            InitializeComponent();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }

        protected String GetAllUser()
        {
            String kq = "";
            int[] s = gvUser.GetSelectedRows();
            if (s.Length > 0)
            {
                for (int i = 0; i < s.Length; i++)
                {
                    DTO.PODInfo inf = new DTO.PODInfo(((DataRowView)gvUser.GetRow(s[i])).Row);
                    kq += "'" + inf.USER_ID + "',";
                }
                if (kq.Length > 0)
                    kq = kq.Remove(kq.Length - 1);
            }

            return kq;
        }

        public DataSet BuildDataSourceNKGiaoDich()
        {
            DataSet _ds = null;
            string query = "";
            if (cbUser.Checked)
            {
                query = @"SELECT     SOCT, LOAICT, NGAYCT, MAKH, TIENHANG, VAT, PHIVC, CHIETKHAU, TONGTIEN, TIENMAT, CONGNO, THUNGAY, LOAITIEN, NameKH, DIACHIKH, DIENTHOAI, 
                      NOIDUNG, TINHTRANG, INHD, TAIKHOAN, NGANHANG, TINH_TP, CREATEBY, MODIFIEDBY, CREATEDATE, MODIFIEDDATE,
                          (SELECT     TENGD
                            FROM          CUSTINFO AS b
                            WHERE      (a.MAKH = CUSTID)) AS TENKH
FROM         GIAODICH AS a
WHERE     (CREATEBY IN ({0})) AND (NGAYCT = '{1:yyyy-MM-dd}')
SELECT     SOCT, LOAICT, NGAYCT, MAKH, TIENHANG, VAT, PHIVC, CHIETKHAU, TONGTIEN, TIENMAT, CONGNO, THUNGAY, LOAITIEN, NameKH, DIACHIKH, DIENTHOAI, 
                      NOIDUNG, TINHTRANG, INHD, TAIKHOAN, NGANHANG, TINH_TP, CREATEBY, MODIFIEDBY, CREATEDATE, MODIFIEDDATE,
                          (SELECT     TENGD
                            FROM          CUSTINFO AS b
                            WHERE      (a.MAKH = CUSTID)) AS TENKH
FROM         delGIAODICH AS a
WHERE     (CREATEBY IN ({0})) AND (NGAYCT = '{1:yyyy-MM-dd}')
";
                String para = GetAllUser();
                query = string.Format(query, para, dtpNgay.EditValue);
            }
            else
            {
                query = @"SELECT     SOCT, LOAICT, NGAYCT, MAKH, TIENHANG, VAT, PHIVC, CHIETKHAU, TONGTIEN, TIENMAT, CONGNO, THUNGAY, LOAITIEN, NameKH, DIACHIKH, DIENTHOAI, 
                      NOIDUNG, TINHTRANG, INHD, TAIKHOAN, NGANHANG, TINH_TP, CREATEBY, MODIFIEDBY, CREATEDATE, MODIFIEDDATE,
                          (SELECT     TENGD
                            FROM          CUSTINFO AS b
                            WHERE      (a.MAKH = CUSTID)) AS TENKH
FROM         GIAODICH AS a
WHERE     (NGAYCT ='{0:yyyy-MM-dd}') 
SELECT     SOCT, LOAICT, NGAYCT, MAKH, TIENHANG, VAT, PHIVC, CHIETKHAU, TONGTIEN, TIENMAT, CONGNO, THUNGAY, LOAITIEN, NameKH, DIACHIKH, DIENTHOAI, 
                      NOIDUNG, TINHTRANG, INHD, TAIKHOAN, NGANHANG, TINH_TP, CREATEBY, MODIFIEDBY, CREATEDATE, MODIFIEDDATE,
                          (SELECT     TENGD
                            FROM          CUSTINFO AS b
                            WHERE      (a.MAKH = CUSTID)) AS TENKH
FROM         delGIAODICH AS a
WHERE     (NGAYCT ='{0:yyyy-MM-dd}')";
                query = string.Format(query, dtpNgay.EditValue);
            }


            BUS.CommonControl ctr = new BUS.CommonControl();
            _ds = ctr.executeSelectQueries(query);
            if (cbUser.Checked)
            {
                query = @"SELECT 	
(select SUM(TIENMAT) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'PC001' and CREATEBY in ({1})) as TIENCHI,
(select SUM(TIENMAT) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'PT001' and CREATEBY in ({1})) as TIENTHU,
(select SUM(TIENMAT) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'UNC01'  and CREATEBY in ({1})) as THUNO,
(select SUM(TIENMAT) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'UNC02' and TAIKHOAN = '' and CREATEBY in ({1})) as UNC,
(select SUM(THUNGAY) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'BH001'  and CREATEBY in ({1})) as THUNGAY,
(select SUM(TIENMAT) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'NH001' and CREATEBY in ({1})) as TRANGAY";
                String para = GetAllUser();
                query = string.Format(query, dtpNgay.EditValue, para);
            }
            else
            {
                query = @"SELECT 	
(select SUM(TIENMAT) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'PC001' ) as TIENCHI,
(select SUM(TIENMAT) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'PT001') as TIENTHU,
(select SUM(TIENMAT) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'UNC01') as THUNO,
(select SUM(TIENMAT) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'UNC02' and TAIKHOAN = '') as UNC,
(select SUM(THUNGAY) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'BH001') as THUNGAY,
(select SUM(TIENMAT) FROM  dbo.[GIAODICH]
WHERE NGAYCT < '{0:yyyy-MM-dd}' and LOAICT = 'NH001') as TRANGAY";
                query = string.Format(query, dtpNgay.EditValue);
            }
            DataTable dt = ctr.executeSelectQuery(query);
            if (_ds.Tables[0].Rows.Count > 0)
            {
                dt.TableName = "Table2";
                _ds.Tables.Add(dt.Copy());
            }
         

            return _ds;

        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            _ds = BuildDataSourceNKGiaoDich();

            if (_ds.Tables.Count == 0)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }
            bool isdata = false;
            foreach (DataTable dt in _ds.Tables)
            {
                if (dt.Rows.Count > 0)
                {
                    isdata = true;
                    break;
                }
            }
            if (isdata == false)
            {
                XtraMessageBox.Show("Dữ liệu không tồn tại!");
                return;
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        protected void LoadUser()
        {
            BUS.PODControl ctr = new BUS.PODControl();
            //gridUser.DataSource = ctr.GetAll("SALE", ref sErr);
            gridUser.DataSource = ctr.GetAll(ref sErr);
        }



        private void frmBangKeGTGT_Load(object sender, EventArgs e)
        {
            dtpNgay.EditValue = frmMain.__ngayMoSo;
            LoadUser();
        }
    }
}